package com.hqx.shiro.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.hqx.shiro.model.Permission;
import com.hqx.shiro.mapper.PermissionMapper;
import com.hqx.shiro.model.RolePermission;
import com.hqx.shiro.model.UserRole;
import com.hqx.shiro.service.*;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author PH
 * @since 2019-07-25
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {

    @Autowired
    private RolePermissionService rolePermissionService;
    @Autowired
    private UserRoleService userRoleService;


    /**
     * 用户可以访问的所有路径
     * @param userId
     * @return
     */
    @Override
    public List<String> userAllPermission(String userId) {
        UserRole userRole = new UserRole();
        userRole.setUserId(userId);
        List<UserRole> userRoleList = userRoleService.selectList(new EntityWrapper<>(userRole));
        List<String> permissionList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(userRoleList)){
            for(UserRole userRoleInfo:userRoleList){
                RolePermission rolePermission = new RolePermission();
                rolePermission.setRoleId(userRoleInfo.getRoleId());
                List<RolePermission> rolePermissionList = rolePermissionService.selectList(new EntityWrapper<>(rolePermission));
                for (RolePermission rolePermissionInfo : rolePermissionList){
                    Permission permission = selectById(rolePermissionInfo.getPermissionId());
                    permissionList.add(permission.getName());
                }
            }

            return permissionList;

        }


        return null;
    }
}
